<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>管理员登录</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/admin.css">
    <link rel="stylesheet" href="css/login.css">
</head>

<body>
<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>欢迎登陆</h2>
            <p>管理员登录平台</p>
        </div>
        <form id="loginForm">
            <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                           for="login-username"></label>
                    <input type="text" name="username" id="login-username"
                           placeholder="用户名" class="layui-input" value="admin">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="login-password"></label>
                    <input type="password" name="password" id="login-password"
                           placeholder="密码" class="layui-input" value="12345">
                </div>
                <div class="layui-form-item">
                    <input type="button" id="loginBtn" class="layui-btn layui-btn-fluid" lay-submit
                           lay-filter="LAY-user-login-submit" value="登 入" />
                </div>
            </div>
        </form>
    </div>

    <div class="layui-trans layadmin-user-login-footer">
        <p>© 2022 zing.online 版权所有 <a href="https://beian.miit.gov.cn" target="_blank">粤ICP备20200774xx号</a></p>
    </div>
</div>

<script src="layui/layui.js"></script>
<script>
    // 加载 LayUI 提供的相关模块
    layui.use(['layer', 'jquery'], function () {
        // 得到模块实例对象
        var layer = layui.layer;
        var $ = layui.jquery;

        // 绑定登录按钮
        $('#loginBtn').on('click', function () {
            login() ;
        });

        // 绑定密码框
        $('#login-password').on('keydown', function (e) {
            if (e.keyCode == 13) {
                login();
            }
        })

        // 登陆
        function login() {
            if ($("#login-username").val() == '' || $("#login-password").val() == '') {
                layer.msg("请输入用户名或密码",{icon: 5},function(){
                    console.log("有毛病，登录肯定需要帐号和密码的")
                });
                return false;
            }

            // 序列化表单 - username=rose&password=123123
            let params = $("#loginForm").serialize();

            // 发起 Ajax 异步请求
            $.ajax({
                url: '/admin/login',
                type: 'post',
                data: params,
                success: function ( res ) {
                    console.log(res)
                    // 登录失败
                    if (res.code != 200) {
                        layer.msg(res.message);
                        return ;
                    }
                    // 登录成功
                    layer.msg(res.message,{icon:6},function(){
                        location.href = res.data;
                    }) ;
                }
            });
        }
    });
</script>
</body>

</html>